{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2dd4e340-6047-4c4e-a3de-df7b02f12629",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from PIL import Image, ImageOps\n",
    "\n",
    "def process_images(input_dir, output_dir):\n",
    "    \"\"\"\n",
    "    处理目录中的所有图片，将较短边缩放至1080像素，保持比例并保存到输出目录\n",
    "    :param input_dir: 待处理图片目录路径\n",
    "    :param output_dir: 输出目录路径\n",
    "    \"\"\"\n",
    "    # 创建输出目录（如果不存在）\n",
    "    os.makedirs(output_dir, exist_ok=True)\n",
    "    \n",
    "    # 支持的图片格式扩展名\n",
    "    supported_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp']\n",
    "    \n",
    "    for filename in os.listdir(input_dir):\n",
    "        file_ext = os.path.splitext(filename)[1].lower()\n",
    "        if file_ext not in supported_extensions:\n",
    "            continue\n",
    "        \n",
    "        input_path = os.path.join(input_dir, filename)\n",
    "        output_path = os.path.join(output_dir, filename)\n",
    "        \n",
    "        try:\n",
    "            with Image.open(input_path) as img:\n",
    "                # 自动修正图片方向\n",
    "                img = ImageOps.exif_transpose(img)\n",
    "                original_width, original_height = img.size\n",
    "                \n",
    "                # 计算新尺寸\n",
    "                if original_width <= original_height:\n",
    "                    new_width = 1080\n",
    "                    new_height = int(original_height * (1080 / original_width))\n",
    "                else:\n",
    "                    new_height = 1080\n",
    "                    new_width = int(original_width * (1080 / original_height))\n",
    "                \n",
    "                # 使用高质量的重采样方法进行缩放\n",
    "                resized_img = img.resize((new_width, new_height), resample=Image.LANCZOS)\n",
    "                \n",
    "                # 根据文件类型设置保存参数\n",
    "                if file_ext in ['.jpg', '.jpeg']:\n",
    "                    resized_img.save(output_path, quality=95, optimize=True)\n",
    "                elif file_ext == '.png':\n",
    "                    resized_img.save(output_path, optimize=True)\n",
    "                else:\n",
    "                    resized_img.save(output_path)\n",
    "                print('程序已正确运行，请到%s目录下查看' % output_path) \n",
    "                \n",
    "        except Exception as e:\n",
    "            print(f\"处理文件 {filename} 时发生错误：{str(e)}\")\n",
    "\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "97999224-dbea-4952-be3a-cedf61f3b190",
   "metadata": {},
   "outputs": [],
   "source": [
    "# oppofindX7U的termux运行\n",
    "input_path = r'/sdcard/personnal_files/upload_picture/Original_drawing_input'\n",
    "output_path = r'/sdcard/personnal_files/upload_picture/1080P_output'\n",
    "process_images(input_path, output_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b3957dc0-8187-4c0b-87ad-b0fa597bbeae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "程序已正确运行，请到C:\\修图后导出\\程序操作专用目录\\程序处理为1080的输出目录\\DSC_1909-已增强-RD.png目录下查看\n",
      "程序已正确运行，请到C:\\修图后导出\\程序操作专用目录\\程序处理为1080的输出目录\\DSC_1922-已增强-RD.png目录下查看\n",
      "程序已正确运行，请到C:\\修图后导出\\程序操作专用目录\\程序处理为1080的输出目录\\DSC_1952-已增强-RD.png目录下查看\n"
     ]
    }
   ],
   "source": [
    "# 联想小新笔记本运行\n",
    "input_path = r'C:\\修图后导出\\程序操作专用目录\\要处理的原图存放目录'\n",
    "output_path = r'C:\\修图后导出\\程序操作专用目录\\程序处理为1080的输出目录'\n",
    "process_images(input_path, output_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ceb84556-780a-4732-93b3-adf82c61e361",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
